<?

/* CharacterController controller class.
 * This class contains the handling code, divided into action methods.
 *
 */ 
class CharacterController extends AGController
{
  function __construct($ctrName) 
  {
    parent::__construct($ctrName);
    // IMPORTANT: don't init CharacterModel explicitly, it's already loaded by $config    
  }

  function Index()
  {
    // show the charater sheet (only displays one's own character)
    $this->pageTitle = $this->character->charDS['c_name'];
    if (isset($_REQUEST['on']))
      $this->character->equip($_REQUEST['on']);
    if (isset($_REQUEST['off']))
      $this->character->equip($_REQUEST['off'], 'unequip');
    if (isset($_REQUEST['buyspell']))
      $this->character->learnSpell($_REQUEST['buyspell']);
    if (isset($_REQUEST['cast']))
      $this->character->castSpell($_REQUEST['cast']);
    $this->mySpells = DB_GetList('SELECT * FROM '.getTableName('spells').'
      LEFT JOIN '.getTableName('character_spells').' ON (cs_character="'.$this->character->charDS['c_key'].'" AND cs_spell=sp_key)
      WHERE cs_key IS NOT NULL AND sp_level <= '.$this->character->getCharacterStat('MP').'
      ORDER BY sp_level,sp_name');
    $this->viewCharacter = &$this->character;
    $this->ownCharacter = true;
  }
  
  function History()
  {
    $this->pageTitle = 'Your History';
    $this->history = DB_GetList('SELECT * FROM '.getTableName('history').' 
      LEFT JOIN '.getTableName('characters').' ON (h_mob=c_key)
      LEFT JOIN '.getTableName('zones').' ON (h_zone=z_key)
      WHERE h_character="'.$this->character->charDS['c_key'].'"
      AND h_type != "duelv"
      AND h_type != "dueld"
      AND h_type != "duelcountd"
      ORDER BY h_last DESC
      LIMIT 500');
  }
  
  function Accomplishments()
  {
    $this->pageTitle = 'Your Accomplishments';
  
  }
  
  function view()
  {
    $this->viewId = $_REQUEST['id'];
    $this->viewCharacter = new CharacterModel('', $this, $this->viewId);
    if (sizeof($this->viewCharacter->charDS)==0)
      $this->redirect('index', 'default');
    $this->pageTitle = $this->viewCharacter->charDS['c_name'].' ('.$this->viewCharacter->level.')';
  }
  
  function reload()
  {
    $this->redirect('index', 'character');
  }
  
  function StatIncrease()
  {
    // this is for AJAX requests, don't return HTML
    $this->pageTemplate = 'blank';
    $this->skipView = true;
    $this->character->increaseStat($_REQUEST['stat']);
  }
}


?>
